<?php
    $login = 'gta1807838';
    $pass = '7x62zUN7kT';
    $domain = 's18.hgs.su';
    
    $data = json_decode($_POST['data'], true);
    
    $nickname = $data['login'];
    $color = $data['color'];
    $message = $data['message'];
    
    $conn = ftp_connect($domain);
    if (ftp_login($conn, $login, $pass)) {
        $remote_file = 'scriptfiles/incoming.txt';
        $file = fopen('temp_incoming', 'a');
        $message = iconv('utf-8', 'CP1251', $message);
        $message = iconv('CP1252', 'utf-8', $message);
        $mytext = "$color|$nickname|$message\n";
        fwrite($file, $mytext);
        fclose($file);
        $file = fopen('temp_incoming', 'r');
        if (ftp_fput($conn, $remote_file, $file, FTP_ASCII)) {
            $result = array(
                "result" => 1,
                "error" => ""
                );
        }
        else {
            $result = array(
                "result" => 0,
                "error" => "Ошибка отправки сообщения"
                );
        }
        fclose($file);
        unlink('temp_incoming');
        echo json_encode($result);
    }